<template>
  <view
    class="contract_box"
    @touchmove="refresh.moveTouchendHandler"
    @touchstart="refresh.startTouchendHandler"
    @touchend="refresh.endTouchendHandler"
  >
    <menu-navigation-bar
      :is-sticky="true"
      :menu-list="menuList"
      @on-selected="onSelected"
    />

    <view class="content">
      <contract-item
        v-for="(item, index) in dataList"
        :key="item.contractualId"
        :data="item"
        @on-click="toDetail(item.contractualId)"
      />

      <!-- 加载状态 -->
      <load :status="loadStatue" :isImage="dataList.length < 1" />
    </view>

    <!-- 下拉刷新 -->
    <pull-down-refresh
      :opacity="refresh.moveBottom.value / 3"
      :enble-anim="refresh.isRefresh.value"
    />
  </view>
</template>

<!-- 合同页面 -->
<script lang="ts" setup>
import { refreshHandler } from '@/components/common/pull_down_refresh/index'
import contractItem from '@/components/business/contract/contract-item.vue'
import { ReqSelectPageContractByState } from '@/api/contract'
import menuNavigationBar from '@/components/layout/menu_navigation_bar/menu-navigation-bar.vue'
import type { PageContract, QueryPageContract } from '@/config/data/data'
import { ContractStateEnum } from '@/config/enum/enum'
import { onLoad } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'

//菜单导航栏
const menuList = ref<string[]>([])

//请求是否结束
const isReq = ref(true)

//菜单变更
const onSelected = (index: number) => {
  queryPage.value.state = menuList.value[index]
  queryPage.value.page = 0
  nextData(true).finally(() => {
    refresh.refreshEnd(300)
  })
}

//下拉刷新控制函数
const refresh = refreshHandler(300)

//监听下拉刷新状态
watch(
  () => refresh.isRefresh.value,
  (newValue, oldValue) => {
    if (newValue) {
      //开启刷新，并请求
      queryPage.value.page = 0
      nextData(true).finally(() => {
        refresh.refreshEnd(500)
      })
    }
  }
)

//初始化
const initData = () => {
  //初始化菜单列表
  for (let msg in ContractStateEnum) {
    if (ContractStateEnum.hasOwnProperty(msg)) {
      menuList.value.push(
        ContractStateEnum[msg as keyof typeof ContractStateEnum]
      )
    }
  }
  queryPage.value.state = menuList.value[0]
  nextData()
}

//分页参数
const queryPage = ref<QueryPageContract>({
  page: 0,
  state: '',
  size: 10,
})

//下一页
const nextData = (refresh: boolean = false) => {
  return new Promise((reslove) => {
    if (!isReq.value) {
      reslove(true)
      return
    }
    isReq.value = false
    queryPage.value.page += 1
    ReqSelectPageContractByState(queryPage.value)
      .then((res) => {
        if (res.code == 200 && res.data) {
          refresh ? (dataList.value = []) : ''
          dataList.value.push(...res.data)
          let bool = queryPage.value.size == res.data.length
          loadStatue.value = bool ? 1 : -1
        }
      })
      .catch(() => {
        loadStatue.value = -2
        setTimeout(() => {
          loadStatue.value = -2
        }, 100)
      })
      .finally(() => {
        isReq.value = true
        reslove(true)
      })
  })
}

/**
 * 跳转详情
 * @param id: 合同id
 */
const toDetail = (id: number) => {
  uni.navigateTo({ url: `/subpkg/contract/detail?id=${id}` })
}

//加载状态
const loadStatue = ref(-1)

//数据列表
const dataList = ref<PageContract[]>([])

//页面加载
onLoad(() => {
  initData()
})
</script>

<style scoped lang="scss">
.contract_box {
  width: 100%;
  min-height: 100vh;
  background-color: $uni-bg-color-grey;

  .content {
    padding: 0 30rpx;
    width: calc(100% - 60rpx);
  }
}
</style>
